package com.neusoft.yuejian12.yuejian_12.activity;

import android.app.Activity;
import android.os.Bundle;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.neusoft.yuejian12.yuejian_12.R;
import com.neusoft.yuejian12.yuejian_12.adapter.SearchTrainAdapter;
import com.neusoft.yuejian12.yuejian_12.javabean.TrainBean;
import com.neusoft.yuejian12.yuejian_12.model.TrainModel;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class SearchTrainActivity extends Activity {
    private EditText search_train;
    private ImageView searchTrain_button;
    private RecyclerView recyclerView;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_search_train);
    initView();

    initEvent();

}

    private void initView() {

        search_train = (EditText) findViewById(R.id.search_train);
        searchTrain_button = (ImageView) findViewById(R.id.searchTrain_button);
        recyclerView = (RecyclerView) findViewById(R.id.searchTrainRV);

    }
    private void initEvent(){
        searchTrain_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String keyword = search_train. getText().toString();
                getData(keyword);
            }
        });
    }
    private void getData(String t_name) {
        TrainModel trainModel=new TrainModel();
        Call<List<TrainBean>> call=trainModel.searchTrain(t_name);
        //System.out.print("t_name");
        call.enqueue(new Callback<List<TrainBean>>() {
            @Override
            public void onResponse(Call<List<TrainBean>> call, Response<List<TrainBean>> response) {
               List<TrainBean> data = response.body();  //获取数据
               // System.out.printf("data");
                recyclerView.setAdapter(new SearchTrainAdapter(SearchTrainActivity.this, data));
            }
            @Override
            public void onFailure(Call<List<TrainBean>> call, Throwable t) {

            }
        });
    }
}

